/*
 * Copyright (c) 2008, Takashi Ito
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the authors nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "tdom-internal.h"
#include "tdom.h"
#include "tdom_writer.h"

static
void dump_tree(struct tdom_doc *doc)
{
    int ret;
    struct evbuffer *buffer = NULL;
    char *line;
    
    if ((buffer = evbuffer_new()) == NULL) {
        return;
    }

    ret = tdom_writer_write_all(buffer, doc);
    if (ret < 0) {
        goto out;
    }

    //printf("ret = %d, len = %d\n", ret, buffer->off);
    while ((line = evbuffer_readline(buffer))) {
        printf("%s\n", line);
        free(line);
    }

out:
    evbuffer_free(buffer);
}

static
void dump_elem(struct tdom_elem *elem, int depth)
{
    struct tdom_node *node;
    struct tdom_ns *ns;
    int i;

    for (i = 0; i < depth; i++) {
        printf("  ");
    }
    printf("%s\n", elem->node.name);

    if (!TAILQ_EMPTY(&elem->nss)) {
        for (i = 0; i < depth; i++) {
            printf("  ");
        }
        printf("[namespaces]\n");
        TAILQ_FOREACH(ns, &elem->nss, sibling) {
            for (i = 0; i < depth; i++) {
                printf("  ");
            }
            printf("%s = %s\n", *ns->prefix ? ns->prefix : "default", ns->uri);
        }
    }

    if (!TAILQ_EMPTY(&elem->attrs)) {
        for (i = 0; i < depth; i++) {
            printf("  ");
        }
        printf("[attributes]\n");
        TAILQ_FOREACH(node, &elem->attrs, sibling) {
            for (i = 0; i < depth; i++) {
                printf("  ");
            }
            printf("%s = %s\n", node->name, ((struct tdom_attr*)node)->value);
        }
    }

    TAILQ_FOREACH(node, &elem->children, sibling) {
        if (node->type == TDOM_ELEM) {
            dump_elem((struct tdom_elem *)node, depth + 1);
        } else if (node->type == TDOM_TEXT) {
            for (i = 0; i < (depth + 1); i++) {
                printf("  ");
            }
            printf("VALUE(%s)\n", ((struct tdom_text *)node)->value);
        } else {
            tdom_assert(0);
        }
    }
}

int main()
{
    int ret;
    char buff[3];

    struct tdom_parser *parser = tdom_parser_new();
    tdom_assert(parser);

    for (;;) {
        int len = fread(buff, 1, sizeof(buff), stdin);
        if (ferror(stdin)) {
            fprintf(stderr, "read error\n");
            exit(-1);
        }
        ret = tdom_parser_parse(parser, buff, len);
        if (ret == 0) {
            continue;
        }
        if (ret > 0) {
            struct tdom_doc *doc = tdom_parser_get_doc(parser);
            printf("--------------------------------------------\n");
            dump_elem(doc->root, 0);
            printf("--------------------------------------------\n");
            dump_tree(doc);
            printf("--------------------------------------------\n");
            tdom_doc_delete(doc);
        } else {
            fprintf(stderr, "tdom_parser_parse() failed %d\n", ret);
        }
        break;
    }

    tdom_parser_delete(parser);

    return 0;
}
